package com.bluegalaxy.sitescore.service.impl;

import com.bluegalaxy.sitescore.entity.*;
import com.bluegalaxy.sitescore.mapper.*;
import com.bluegalaxy.sitescore.service.SurfaceWaterGWService;
import com.bluegalaxy.sitescore.service.support.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public  class SurfaceWaterGWServiceImpl extends BaseServiceImpl<TbGroundWaterToSwMapper,TbGroundWaterToSw> implements SurfaceWaterGWService {

    @Autowired
    private TbGroundWaterToSwMapper tbGroundWaterToSwMapper ;

    @Autowired
    private TbGroundWaterDrinkingWaterMapper tbGroundWaterDrinkingWaterMapper;

    @Autowired
    private TbGroundWaterHumanFoodChainMapper tbGroundWaterHumanFoodChainMapper;

    @Autowired
    private TbGroundWaterEnvironmentMapper tbGroundWaterEnvironmentMapper;

    @Override
    public BigDecimal caculateScoreGroud(SurfaceWaterGW surfaceWaterOL,int sourceId) {
        Date date = new Date();
        BigDecimal release = BigDecimal.valueOf(surfaceWaterOL.getContainment())
                .multiply(BigDecimal.valueOf(surfaceWaterOL.getNet_precip()).add(
                        BigDecimal.valueOf(surfaceWaterOL.getDepth_to_auuifer()).add(
                                BigDecimal.valueOf(surfaceWaterOL.getTravel_time())
                        )
                ));

        if (release.compareTo(BigDecimal.valueOf(500))==1){
            release = BigDecimal.valueOf(500);
        }
        if (surfaceWaterOL.getObserved_release()==550){
            release = BigDecimal.valueOf(550);
        }
        BigDecimal dwTarget = BigDecimal.valueOf(surfaceWaterOL.getPop_concent1()*10).add(
                BigDecimal.valueOf(surfaceWaterOL.getPop_concent2()).add(
                        BigDecimal.valueOf(surfaceWaterOL.getPotential_contanmination()*0.1))).add(
                BigDecimal.valueOf(surfaceWaterOL.getNearest_intake())).add(
                BigDecimal.valueOf(surfaceWaterOL.getResource()));
        BigDecimal dwUScore = release.multiply(dwTarget).multiply(
                BigDecimal.valueOf(surfaceWaterOL.getWaste_characteristics())).divide(
                BigDecimal.valueOf(82500),2, RoundingMode.DOWN);
        BigDecimal dwScore = null;
        if (dwUScore.compareTo(BigDecimal.valueOf(100))==1){
            dwScore = BigDecimal.valueOf(100);
        }else {
            dwScore = dwUScore;
        }
        BigDecimal hfTarget = BigDecimal.valueOf(surfaceWaterOL.getPop_concent12()*10).add(
                BigDecimal.valueOf(surfaceWaterOL.getPop_concent22()).add(
                        BigDecimal.valueOf(surfaceWaterOL.getPot_HFC_concent()*0.1))).add(
                BigDecimal.valueOf(surfaceWaterOL.getFood_chain_individual()));

        BigDecimal hfUScore = release.multiply(hfTarget).multiply(
                BigDecimal.valueOf(surfaceWaterOL.getWaste_characteristics2())).divide(
                BigDecimal.valueOf(82500),2, RoundingMode.DOWN);

        BigDecimal hfScore = null;
        if (hfUScore.compareTo(BigDecimal.valueOf(100))==1){
            hfScore = BigDecimal.valueOf(100);
        }else {
            hfScore = hfUScore;
        }
        BigDecimal eTarget = BigDecimal.valueOf(surfaceWaterOL.getPop_concent13()*10).add(
                BigDecimal.valueOf(surfaceWaterOL.getPop_concent23()).add(
                        BigDecimal.valueOf(surfaceWaterOL.getPotential_contanmination3()*0.1)));

        BigDecimal eUScore = release.multiply(eTarget).multiply(
                BigDecimal.valueOf(surfaceWaterOL.getWaste_cha())).divide(
                BigDecimal.valueOf(82500),2, RoundingMode.DOWN);

        BigDecimal eScore = null;
        if (eUScore.compareTo(BigDecimal.valueOf(100))==1){
            eScore = BigDecimal.valueOf(100);
        }else {
            eScore = eUScore;
        }
        BigDecimal score = dwScore.add(hfScore).add(eScore);
        if (score.compareTo(BigDecimal.valueOf(100))==1){
            score = BigDecimal.valueOf(100);
        }
       TbGroundWaterToSw tbGroundWaterToSw = new TbGroundWaterToSw();
        tbGroundWaterToSw.setSorce_id(sourceId);
        tbGroundWaterToSw.setWatershed_name(surfaceWaterOL.getWaterShed_name());
        tbGroundWaterToSw.setContainment(surfaceWaterOL.getContainment());
        tbGroundWaterToSw.setCreate_time(date);
        tbGroundWaterToSw.setDepth_to_auuifer(surfaceWaterOL.getDepth_to_auuifer());
        tbGroundWaterToSw.setLikeihood_release(surfaceWaterOL.getLikeihood_release());
        tbGroundWaterToSw.setIs_delete(0);
        tbGroundWaterToSw.setNet_precip(surfaceWaterOL.getNet_precip());
        tbGroundWaterToSw.setObserved_release(surfaceWaterOL.getObserved_release());
        tbGroundWaterToSw.setPotential_to_release(surfaceWaterOL.getPotential_to_release());
        tbGroundWaterToSw.setTravel_time(surfaceWaterOL.getTravel_time());
        tbGroundWaterToSw.setUpdate_time(date);
        tbGroundWaterToSw.setWatershed_score(surfaceWaterOL.getWatershed_score().floatValue());
        //显示时应单独额外计算
        tbGroundWaterToSw.setMigration_score(surfaceWaterOL.getUncapped_score().floatValue());
        if(surfaceWaterOL.getId()==0) {
            tbGroundWaterToSwMapper.insertGetId(tbGroundWaterToSw);
        }else{
            tbGroundWaterToSw.setId(surfaceWaterOL.getId());
            tbGroundWaterToSwMapper.updateGroundWater(tbGroundWaterToSw);
        }
        int id = tbGroundWaterToSw.getId();
       // System.out.println(id);

        TbGroundWaterDrinkingWater tbGroundWaterDrinkingWater = new TbGroundWaterDrinkingWater();
        tbGroundWaterDrinkingWater.setSource_id(sourceId);
        tbGroundWaterDrinkingWater.setGround_water_id(id);
        //不知是啥
        tbGroundWaterDrinkingWater.setMobility_type(1);
        tbGroundWaterDrinkingWater.setWater_category(1);
        tbGroundWaterDrinkingWater.setHaza_quantity(surfaceWaterOL.getHaza_quantity());
        tbGroundWaterDrinkingWater.setWaste_characteristics(surfaceWaterOL.getWaste_characteristics());

        tbGroundWaterDrinkingWater.setNearest_intake(surfaceWaterOL.getNearest_intake());
        tbGroundWaterDrinkingWater.setPop_concent1(surfaceWaterOL.getPop_concent1());
        tbGroundWaterDrinkingWater.setPop_concent2(surfaceWaterOL.getPop_concent2());
        tbGroundWaterDrinkingWater.setPotential_contanmination(surfaceWaterOL.getPotential_contanmination());
        //这个应为float
        tbGroundWaterDrinkingWater.setPopulation(surfaceWaterOL.getPopulation().floatValue());
        tbGroundWaterDrinkingWater.setResource(surfaceWaterOL.getResource());
        //这两个应为float
        tbGroundWaterDrinkingWater.setTarget(surfaceWaterOL.getTarget().floatValue());
        tbGroundWaterDrinkingWater.setDrinking_water_score(surfaceWaterOL.getDrinking_water_score().floatValue());
      /*  System.out.println(dwScore);
        System.out.println("float:"+dwScore.floatValue());
        System.out.println("get:"+tbGroundWaterDrinkingWater.getDrinking_water_score());*/
        tbGroundWaterDrinkingWater.setUncapped_score(surfaceWaterOL.getDrinking_water_score().floatValue());
        tbGroundWaterDrinkingWater.setIs_delete(0);
        if(surfaceWaterOL.getId()==0) {
            tbGroundWaterDrinkingWater.setCreate_time(date);
            tbGroundWaterDrinkingWater.setUpdate_time(date);
            tbGroundWaterDrinkingWaterMapper.insert(tbGroundWaterDrinkingWater);
        }else {
            tbGroundWaterDrinkingWater.setUpdate_time(date);
            tbGroundWaterDrinkingWaterMapper.updateBySid(tbGroundWaterDrinkingWater);
        }

        //食物链
        TbGroundWaterHumanFoodChain tbGroundWaterHumanFoodChain = new TbGroundWaterHumanFoodChain();
        tbGroundWaterHumanFoodChain.setSource_id(sourceId);
        tbGroundWaterHumanFoodChain.setGround_water_id(id);
        //不知是啥
        tbGroundWaterHumanFoodChain.setOver_load_id(1);
        tbGroundWaterHumanFoodChain.setMobility_type(1);
        tbGroundWaterHumanFoodChain.setWater_category(1);
        tbGroundWaterHumanFoodChain.setBody_type(1);
        //应为float
        tbGroundWaterHumanFoodChain.setBioa_val(surfaceWaterOL.getBioa_val().floatValue());
        tbGroundWaterHumanFoodChain.setHaz_waster_quantity(surfaceWaterOL.getHaza_quantity2());
        tbGroundWaterHumanFoodChain.setWaste_cha(surfaceWaterOL.getWaste_cha());
        tbGroundWaterHumanFoodChain.setFood_chain_individual(surfaceWaterOL.getFood_chain_individual());
        tbGroundWaterHumanFoodChain.setPop_concent1(surfaceWaterOL.getPop_concent12());
        tbGroundWaterHumanFoodChain.setPop_concent2(surfaceWaterOL.getPop_concent22());
        tbGroundWaterHumanFoodChain.setPot_HFC_concent(surfaceWaterOL.getPot_HFC_concent());
        //这两个应为float
        tbGroundWaterHumanFoodChain.setPopulation(surfaceWaterOL.getPopulation2().floatValue());
        tbGroundWaterHumanFoodChain.setTargets(surfaceWaterOL.getTarget2().floatValue());
        tbGroundWaterHumanFoodChain.setHuman_food_chain_score(surfaceWaterOL.getHuman_food_chain_score().floatValue());
        tbGroundWaterHumanFoodChain.setUncapped_score(surfaceWaterOL.getHuman_food_chain_score().floatValue());
        tbGroundWaterHumanFoodChain.setIs_delete(0);
        if(surfaceWaterOL.getId()==0) {
            tbGroundWaterHumanFoodChain.setCreate_time(date);
            tbGroundWaterHumanFoodChain.setUpdate_time(date);
            tbGroundWaterHumanFoodChainMapper.insert(tbGroundWaterHumanFoodChain);
        }else {
            tbGroundWaterHumanFoodChain.setUpdate_time(date);
            tbGroundWaterHumanFoodChainMapper.updateByGid(tbGroundWaterHumanFoodChain);
        }

        //环境
        TbGroundWaterEnvironment tbGroundWaterEnvironment = new TbGroundWaterEnvironment();
        tbGroundWaterEnvironment.setSource_id(sourceId);
        tbGroundWaterEnvironment.setGround_water_id(id);
        //不知是啥
        tbGroundWaterEnvironment.setTox_body_type(1);
        tbGroundWaterEnvironment.setPersistence(surfaceWaterOL.getPersistence3().floatValue());
        //应为float
        tbGroundWaterEnvironment.setEcosystem_bap_value(surfaceWaterOL.getEcosystem_bap_value().floatValue());
        tbGroundWaterEnvironment.setHaz_waster_quantity(surfaceWaterOL.getHaz_waster_quantity());
        tbGroundWaterEnvironment.setWaste_cha(surfaceWaterOL.getWaste_cha());
        //重复字段,且为float
        tbGroundWaterEnvironment.setSensitive_environment(surfaceWaterOL.getPop_sensitive_environments().floatValue());
        tbGroundWaterEnvironment.setPop_concent1(surfaceWaterOL.getPop_concent13());
        tbGroundWaterEnvironment.setPop_concent2(surfaceWaterOL.getPop_concent23());
        tbGroundWaterEnvironment.setPotential_contanmination(surfaceWaterOL.getPotential_contanmination());
        //应为float值
        tbGroundWaterEnvironment.setPop_sensitive_environments(surfaceWaterOL.getPop_sensitive_environments().floatValue());
        tbGroundWaterEnvironment.setResources(surfaceWaterOL.getResource());
        //此两个也应为float
        tbGroundWaterEnvironment.setEnvironment_score(surfaceWaterOL.getEnvironment_score().floatValue());
        tbGroundWaterEnvironment.setUncapped_score(surfaceWaterOL.getEnvironment_score().floatValue());
        tbGroundWaterEnvironment.setIs_delete(0);
        if(surfaceWaterOL.getId()==0) {
            tbGroundWaterEnvironment.setCreate_time(date);
            tbGroundWaterEnvironment.setUpdate_time(date);
            tbGroundWaterEnvironmentMapper.insert(tbGroundWaterEnvironment);
        }else {
            tbGroundWaterEnvironment.setUpdate_time(date);
            tbGroundWaterEnvironmentMapper.updateBySid(tbGroundWaterEnvironment);
        }
        return score;
    }


    @Override
    public boolean judgeScoreGroud(SurfaceWaterGW surfaceWaterOL) {
        if (surfaceWaterOL.getObserved_release()!=550&&surfaceWaterOL.getObserved_release()!=0){
            return  false;
        }else if (surfaceWaterOL.getContainment()!=0&&surfaceWaterOL.getContainment()!=3&&
                surfaceWaterOL.getContainment()!=5&&surfaceWaterOL.getContainment()!=7&&
                surfaceWaterOL.getContainment()!=9&&surfaceWaterOL.getContainment()!=10){
            return false;
        }else if (surfaceWaterOL.getNet_precip()!=0&&surfaceWaterOL.getNet_precip()!=1&&
                surfaceWaterOL.getNet_precip()!=3&&surfaceWaterOL.getNet_precip()!=6&&
                surfaceWaterOL.getNet_precip()!=10){
            return false;
        }else if (surfaceWaterOL.getDepth_to_auuifer()!=1&&surfaceWaterOL.getDepth_to_auuifer()!=3&&
                surfaceWaterOL.getDepth_to_auuifer()!=5){
            return false;
        }else if (surfaceWaterOL.getTravel_time()!=1&&surfaceWaterOL.getTravel_time()!=5
                &&surfaceWaterOL.getTravel_time()!=15&&surfaceWaterOL.getTravel_time()!=25
                &&surfaceWaterOL.getTravel_time()!=35) {
            return false;
        }

        return true;
    }

    @Override
    public List<TbScore> getSWGroundList(int sourceId) {
        List<TbScore> tbGroundWaterToSws = new ArrayList<>();
        TbGroundWaterToSw tbGroundWaterToSw = new TbGroundWaterToSw();
        tbGroundWaterToSw.setSorce_id(sourceId);
        tbGroundWaterToSw.setIs_delete(0);
        if (tbGroundWaterToSwMapper.selectCount(tbGroundWaterToSw)==0){
            return tbGroundWaterToSws;
        }
         tbGroundWaterToSws = tbGroundWaterToSwMapper.getList(sourceId);
        tbGroundWaterToSws.get(0).setUsed(true);
        return tbGroundWaterToSws;
    }

    @Override
    public SurfaceWaterGW getGroundSurfaceById(int id) {
        return tbGroundWaterToSwMapper.getGroundSurfaceById(id);
    }

    @Override
    public boolean delScore(int id,int sourceId) {
        if (tbGroundWaterToSwMapper.delSurfaceWaterGW(id,sourceId)){
            return true;
        }else {
            return false;
        }
    }
}
